﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Telnet UI
 * Version 1.00
 * 
 * Connection Definition Dialog Class
 *      Provides a dialog definition for creating or editing a new connection definition.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Windows.Forms;
using Adaptive.Foundation.Windows.Forms;

namespace Adaptive.Telnet.UI
{
    /// <summary>
    /// Provides a dialog definition for creating or editing a new connection definition.
    /// </summary>
    public partial class ConnectionDefinitionDialog : ModalDialogBase
    {
        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ConnectionDefinitionDialog()
            : base()
        {
            InitializeComponent();

        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }

            components = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the value of the address to connect to.
        /// </summary>
        /// <value>
        /// A remopte host name / domain name or IP address.
        /// </value>
        public string Address
        {
            get{return AddressText.Text;}
            set{AddressText.Text = value;}
        }
        /// <summary>
        /// Gets or sets the description associated with the connection definition.
        /// </summary>
        /// <value>
        /// Description text or a blank string.
        /// </value>
        public string Description
        {
            get{return DescriptionText.Text;}
            set { DescriptionText.Text = value; }
        }
        /// <summary>
        /// Gets or sets the name associated with the connection definition.
        /// </summary>
        /// <value>
        /// The name to be associated with the definition.
        /// </value>
        public string DefinitionName
        {
            get{return NameText.Text;}
            set { NameText.Text = value; }
        }
        /// <summary>
        /// Gets or sets the Internet port number to connect on.
        /// </summary>
        /// <value>
        /// An Internet port number in the range 0 - 65535.
        /// </value>
        public short Port
        {
            get { return (short)PortText.ToInteger(); }
            set { PortText.Value = value; }
        }
        /// <summary>
        /// Gets or sets a value indicating whether to send characters immediately or 
        /// wait for enter to be pressed.
        /// </summary>
        /// <value>
        /// <b>true</b> to send characters immediately;
        /// <b>false</b> to wait until the Enter key is pressed.
        /// </value>
        public bool SendCharactersAsTyped
        {
            get
            {
                return SendCharactersOption.Checked;
            }

            set 
            {
                SendCharactersOption.Checked = value;
                SendLineOption.Checked = !value;
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Creates and assigns the event handlers for the controls on the dialog.
        /// </summary>
        protected override void AssignEventHandlers()
        {
            //Buttons.
            OkButton.Click +=new EventHandler(HandleOkClick);
            CloseButton.Click +=new EventHandler(HandleCloseClick);
        }
        /// <summary>
        /// Removes the event handlers for the controls on the dialog.
        /// </summary>
        protected override void RemoveEventHandlers()
        {
            //Buttons.
            OkButton.Click -= new EventHandler(HandleOkClick);
            CloseButton.Click -= new EventHandler(HandleCloseClick);
        }
        /// <summary>
        /// Validates the contents of the dialog.
        /// </summary>
        /// <returns>
        /// <b>true</b> if the item entries are valid; otherwise, returns <b>false</b>.
        /// </returns>
        public override bool ValidateChildren()
        {
            bool returnValue = false;
            string message = string.Empty;

            if (NameText.Text.Length == 0)
                message = "Please enter a name for this connection definition.";
            else if (AddressText.Text.Length == 0)
                message = "Please enter a valid host or domain name or IP address for the connection.";
            else if ((PortText.ToInteger() <= 0) || (PortText.ToInteger() >= 65536))
                message = "Please enter a valid Internet port number.";
            else
                returnValue = true;

            if (!returnValue)
            {
                MessageBox.Show(message, "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return returnValue;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        /// <summary>
        /// Handles the event when the OK button is clicked.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleOkClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            if (ValidateChildren())
            {
                DialogResult = DialogResult.OK;
                Close();
            }
            else
                Cursor = Cursors.Default;
        }
        /// <summary>
        /// Handles the event when the Cancel button is clicked.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleCloseClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            DialogResult = DialogResult.Cancel;
            Close();
        }
        #endregion
    }
}
